<?php
include 'int.php';

if (isset($_POST["session"]) && $_POST["session"]!=""){
	if ($stmt = $mysqlpdo->prepare("SELECT g.u_num, g.user FROM join_game g WHERE g.session=?")) {
		$stmt->bindParam(1, $_POST["session"], PDO::PARAM_STR);
		$stmt->execute();
		if ($stmt->rowCount()==0){
			echo "error";
			exit();
		}
		$current_user = $stmt->fetch(PDO::FETCH_BOTH);
	}else{
		print("<div style=\"errmsg\">Error in SQL1</div>");
		exit();
	}
	
	if ($stmt2 = $mysqlpdo->prepare("SELECT flag FROM system_flag WHERE name='game_start'")){
		$stmt2->execute();
		$result = $stmt2->fetch(PDO::FETCH_BOTH);
		if ($result[0]==1){
			echo "start";
			exit();
		}
	}else{
		echo "error1\n\r";
		exit();
	}
	
	if ($stmt2 = $mysqlpdo->prepare("SELECT * FROM join_game")){
		$stmt2->execute();
		$u_num = array();
		$joined = array();
		$user = array();
		$image = array();
		while($result = $stmt2->fetch(PDO::FETCH_BOTH)){
			array_push($u_num, $result["u_num"]);
			array_push($joined, $result["joined"]);
			array_push($user, $result["user"]);
			array_push($image, $result["photo"]);
		}
		echo json_encode(array("u_num"=>$u_num,"joined"=>$joined,"user"=>$user,"current_u_num"=>$current_user[0],
		"current_user"=>$current_user[1],"image"=>$image))."\n\r";
	}else{
		print("<div style=\"errmsg\">Error in SQL2</div>");
		exit();
	}
	if ($stmt3 = $mysqlpdo->prepare("UPDATE join_game g SET g.timeout=NOW() WHERE g.session=?")){
		$stmt3->bindParam(1, $_POST["session"], PDO::PARAM_STR);
		$stmt3->execute();
	}else{
		print("<div style=\"errmsg\">Error in SQL3</div>");
		exit();
	}
}
?>